﻿//创建时间: 2010-12-15
//创建作者: 李晓忠
//功能说明: 广告信息保存
//最后修改: 2010-12-15
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using TRPOP.Common;
using TRPOP.CMS.Web.UI;
using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using System.Collections;
using Newtonsoft.Json;

namespace TRPOP.CMS.Web.Admin
{
    public partial class advertisement_save : BaseAdmin
    {
        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_strPagePermissionExtraKey = "Plugin_Advertisement";
        }
        #endregion

        #region 页面变量
        protected string m_Title = "广告信息保存";
        protected string m_strRefer = "advertisement_list.aspx";
        protected int intId = 0;
        protected AdvscontentManage mAdvscontentManage = new AdvscontentManage();
        protected AdvscontentInfo mAdvscontentInfo = new AdvscontentInfo();
        protected string m_ConfigTypeKey = "Advertisement";
        private ContentUpFileManage mContentUpFileManage = new ContentUpFileManage();
        #endregion

        #region 页面事件

        protected void Page_Load(object sender, EventArgs e)
        {

            intId = RequestHelper.RequestInt("id");

            if (!IsPostBack)
            {
                LoadBase();
                BindData();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            #region 验证
            if (txtPageName.Text.Trim() == "")
            {
                Alert("请输入生成文件名");
                return;
            }
            if (txtName.Text.Trim() == "")
            {
                Alert("请输入广告名称");
                return;
            }
            if (txtDetail.Text.Trim() == "")
            {
                Alert("请输入广告内容");
                return;
            }

            AdvscontentInfo pageNameInfo = mAdvscontentManage.GetInfoByPageName(txtPageName.Text);
            if (pageNameInfo != null)
            {
                if (intId > 0)
                {
                    if (pageNameInfo.AdID != intId)
                    {
                        Alert("输入生成文件名已存在，请修改");
                        return;
                    }
                }
                else
                {
                    Alert("输入生成文件名已存在，请修改");
                    return;
                }
            }
            #endregion

            if (intId > 0)
            {
                mAdvscontentInfo = mAdvscontentManage.GetInfoById(intId);
                mAdvscontentInfo.UpdateTime = DateTime.Now;
                mAdvscontentInfo.UpdateUserId = base.mUserBaseInfo.UserBaseId;

            }
            else
            {
                mAdvscontentInfo = new AdvscontentInfo();
                mAdvscontentInfo.UpdateTime = DateTime.Now;
                mAdvscontentInfo.UpdateUserId = base.mUserBaseInfo.UserBaseId;
                mAdvscontentInfo.AddTime = DateTime.Now;
                mAdvscontentInfo.AddUserId = base.mUserBaseInfo.UserBaseId;
            }

            mAdvscontentInfo.Detial = txtDetail.Text;
            mAdvscontentInfo.CustomersID = 0;
            mAdvscontentInfo.IsValid = rbtIsValid.SelectedValue == "1";
            mAdvscontentInfo.ShowModel = rbtShowModel.SelectedValue.ToInt();
            mAdvscontentInfo.Name = txtName.TextFilterSQL;
            mAdvscontentInfo.PageName = txtPageName.TextFilterSQL;
            mAdvscontentInfo.Redirect = txtRedirect.TextFilterSQL;

            try
            {
                mAdvscontentInfo.MaturityTime = Convert.ToDateTime(string.Format("{0} {1}:{2}:{3}", txtMaturityTime.Text, ddlHour.SelectedValue, ddlMinute.SelectedValue, ddlSecond.SelectedValue));
            }
            catch
            {
                mAdvscontentInfo.MaturityTime = StringExtension.NullDateTime;
            }
            mAdvscontentManage.Save(mAdvscontentInfo);
            mAdvscontentManage.UpdateAfterSave(mAdvscontentInfo, false);
            mAdvscontentManage.UpdateJSFile(mAdvscontentInfo);
            mContentUpFileManage.SaveEditorUpLoadFile(PageAssist.EditorUpLoadFieldKey, base.mUserBaseInfo.UserBaseId, AttributeType.EnumElement.Ads);
            base.SaveLog(Plugin.LogKeys.OperObject.PluginOper, Plugin.LogKeys.OperResult.EditSubmit, m_ConfigTypeKey);
            SubmitAlertAndThisNewLocation(EnumSubmitResult.Success, m_strRefer);

        }

        #endregion

        #region 页面方法

        void LoadBase()
        {

            #region 时间
            DateTime dt = DateTime.Now.AddYears(1);
            txtMaturityTime.Text = dt.ToString("yyyy-MM-dd");
            ddlHour.Items.Clear();
            for (int i = 0; i < 24; i++)
            {
                string s = i < 9 ? "0" + i.ToString() : i.ToString();
                ddlHour.Items.Add(new ListItem(s, i.ToString()));
            }
            ddlHour.SelectedValue = dt.Hour.ToString();
            ddlMinute.Items.Clear();
            for (int i = 0; i < 60; i++)
            {
                string s = i < 9 ? "0" + i.ToString() : i.ToString();
                ddlMinute.Items.Add(new ListItem(s, i.ToString()));
            }
            ddlMinute.SelectedValue = dt.Minute.ToString();
            ddlSecond.Items.Clear();
            for (int i = 0; i < 60; i++)
            {
                string s = i < 9 ? "0" + i.ToString() : i.ToString();
                ddlSecond.Items.Add(new ListItem(s, i.ToString()));
            }
            ddlSecond.SelectedValue = dt.Second.ToString();
            #endregion

            #region 设置上传目录
            JavaScriptObject jsNode = new JavaScriptObject();
            PSNManage mPsn = new PSNManage();
            string strUpLoadRoot = mPsn.GetPSNPathByPSN(SYSConfig.AdvertisementConfigInfo.ResourceRoot);
            jsNode.Add("WaterPic", "");
            jsNode.Add("savepath", strUpLoadRoot);
            jsNode.Add("nodeid", "0");
            Session.Add("jsNode", jsNode);
            #endregion

        }

        void BindData()
        {

            if (intId > 0)
            {
                mAdvscontentInfo = mAdvscontentManage.GetInfoById(intId);
                if (mAdvscontentInfo != null)
                {
                    txtDetail.Text = mAdvscontentInfo.Detial;
                    txtMaturityTime.Text = mAdvscontentInfo.MaturityTime.ToString("yyyy-MM-dd");
                    ddlHour.SelectedValue = mAdvscontentInfo.MaturityTime.Hour.ToString();
                    ddlMinute.SelectedValue = mAdvscontentInfo.MaturityTime.Minute.ToString();
                    ddlSecond.SelectedValue = mAdvscontentInfo.MaturityTime.Second.ToString();
                    txtPageName.Text = mAdvscontentInfo.PageName;
                    txtName.Text = mAdvscontentInfo.Name;
                    rbtIsValid.SelectedValue = mAdvscontentInfo.IsValid ? "1" : "0";
                    rbtShowModel.SelectedValue = mAdvscontentInfo.ShowModel.ToString();
                    txtHits.Text = mAdvscontentInfo.Hits.ToString();
                    trHits.Visible = true;
                    txtRedirect.Text = mAdvscontentInfo.Redirect;
                }
                else
                {
                    Alert("请通过正确的途径进入本页面");
                    base.ExecuteJs("window.location.href='" + m_strRefer + "'");
                    return;
                }
            }
            else
            {
                txtPageName.Text = (mAdvscontentManage.GetMaxId() + 1).ToString();
                rbtShowModel.SelectedValue = SYSConfig.AdvertisementConfigInfo.Mode.ToString();
            }

            btnSubmit.Enabled = SYSConfig.AdvertisementConfigInfo.Enable;
        }
        #endregion
    }
}
